/**
 * <p>Copyright(C) 2018 Hangzhou Differsoft Co., Ltd. All rights reserved.</p>
 */
package com.rede.didiok.common.utils;

import java.lang.reflect.Field;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * <p>
 * 类描述: 集合工具类
 * </p>
 *
 * @since 2018年10月10日
 * @author wengjiaxin
 */
public class ListUtils {
    private static final int RESULT_INITIAL_SIZE = 10;
    private static final String NULL = "null";
    private static final Logger LOG = LoggerFactory.getLogger(ListUtils.class);

    private ListUtils() {
    }

    /**
     * 判断一个List是否为null或者空.
     *
     * ListUtils.isEmpty(null) = true ListUtils.isEmpty([]) = true ListUtils.isEmpty([null]) = true
     * ListUtils.isEmpty([1]) = false ListUtils.isEmpty([1,2]) = false
     *
     * @param list
     *            需要判断的List
     * @param <T>
     *            泛型
     * @return 判断结果
     */
    public static <T> boolean isEmpty(List<T> list) {
        boolean isEmpty = true;
        if (list != null && !list.isEmpty() && !(list.size() == 1 && NULL.equals(list.get(0)))) {
            isEmpty = false;
        }
        return isEmpty;
    }

    /**
     * 判断一个List是否不为null且不为空.
     *
     * ListUtils.isNotEmpty(null) = false ListUtils.isNotEmpty([]) = false
     * ListUtils.isNotEmpty([null]) = false ListUtils.isNotEmpty([1]) = true
     * ListUtils.isNotEmpty([1,2]) = true
     *
     * @param list
     *            需要判断的List
     * @param <T>
     *            泛型
     * @return 判断结果
     */
    public static <T> boolean isNotEmpty(List<T> list) {
        return !isEmpty(list);
    }

    /**
     * 
     * @param source
     *            List<T>
     * @param fields
     *            属性名
     * @param <T>
     *            泛型
     * @return List<Map<String, Object>>
     */
    public static <T> List<Map<String, Object>> convertToMapList(List<T> source, String[] fields) {
        if (ListUtils.isEmpty(source)) {
            return new ArrayList<>();
        }

        List<String> fieldList = Arrays.asList(fields);
        T t = source.get(0);
        Field[] queryFields = t.getClass().getDeclaredFields();
        Field[] queryBaseFields = t.getClass().getSuperclass().getDeclaredFields();
        Field[] totalFields = new Field[queryFields.length + queryBaseFields.length];
        System.arraycopy(queryFields, 0, totalFields, 0, queryFields.length);
        System.arraycopy(queryBaseFields, 0, totalFields, queryFields.length, queryBaseFields.length);
        List<Map<String, Object>> list = new ArrayList<>(source.size());
        for (T src : source) {
            Map<String, Object> result = new HashMap<>(RESULT_INITIAL_SIZE);
            for (Field field : totalFields) {
                try {
                    field.setAccessible(true);
                    String fieldName = field.getName();
                    Object value = field.get(src);
                    if (fieldList.contains(fieldName)) {
                        result.put(fieldName, value);
                    }

                } catch (IllegalAccessException e) {
                    LOG.error("IllegalAccessException 错误 {}", e.getMessage());

                }
            }
            list.add(result);
        }
        return list;
    }

}
